package com.quan.hr.web.controller;

import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.imports.ExcelImportService;
import cn.afterturn.easypoi.exception.excel.ExcelImportException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.annotation.OperationLog;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.interceptor.LoginUserInterceptor;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.vo.R;
import com.quan.hr.commons.export.HrPostImportVo;
import com.quan.hr.commons.vo.HrPostVo;
import com.quan.hr.entity.HrPost;
import com.quan.hr.service.HrPostService;
import com.quan.system.commons.vo.SysUserVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;


/**
 * 人事管理-岗位信息
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2023-02-21 16:41:34
 */
@Slf4j
@RestController
@RequestMapping("/hr/hrpost")
public class Upload extends MyBaseController {


    /**
     * 多文件上传
     * @param request
     * @return
     */
    @PostMapping(value = "/importExcelPost")
    public R importExcelPost(HttpServletRequest request) {
        // 选择Sheet,params默认选择第一个sheet
        ImportParams params = new ImportParams();

        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

        // 取得MultiValueMap集合
        MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
        Set<Map.Entry<String, List<MultipartFile>>> entrySet = multiFileMap.entrySet();
        for (Map.Entry<String, List<MultipartFile>> entry : entrySet) {
            // 获取MultipartFile集合
            List<MultipartFile> multipartFiles = entry.getValue();
            for (MultipartFile multipartFile : multipartFiles) {

                InputStream inputStream = null;
                try {
                    inputStream = multipartFile.getInputStream();

                    final ExcelImportService excelImportService = new ExcelImportService();

                    final List<HrPostImportVo> list = excelImportService.importExcelByIs(inputStream, HrPostImportVo.class, params, false).getList();

                    list.forEach(obj -> System.out.println(obj));

                } catch (ExcelImportException e) {
                    throw new ExcelImportException(e.getType(), e);
                } catch (Exception e) {
                    throw new ExcelImportException(e.getMessage(), e);
                } finally {
                    IOUtils.closeQuietly(inputStream);
                }
            }
        }

        return R.ok();
    }


    /**
     * 单文件上传
     * @param request
     * @return
     */
    @PostMapping(value = "/importExcelPost")
    public R importExcelPost(@RequestParam(value = "file")MultipartFile multipartFile, HttpServletRequest request) {
        InputStream inputStream = null;
        try {
            inputStream = multipartFile.getInputStream();

            ImportParams params = new ImportParams();

            final List<HrPostImportVo> list = new ExcelImportService().importExcelByIs(inputStream, HrPostImportVo.class, params, false).getList();

            list.forEach(obj -> System.out.println(obj));

        } catch (ExcelImportException e) {
            throw new ExcelImportException(e.getType(), e);
        } catch (Exception e) {
            throw new ExcelImportException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }

        return R.ok();
    }

}
